package com.may.wlrj.services;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;

import com.may.wlrj.dao.LockDAO;
import com.may.wlrj.dao.MissionDAO;
import com.may.wlrj.entity.MissionEntity;

@Service
public class MissionCheckService {
	private Logger log = Logger.getLogger(MissionCheckService.class);
	private MissionDAO missionDao;
	@Autowired
	public void setMissionDAO(MissionDAO missionDao) {
		this.missionDao = missionDao;
	} 
	
	private LockDAO lockDao;
	
    @Autowired
    public void setlockDao(LockDAO lockDao){
        this.lockDao =lockDao;
    }
    
	//定时执行的方法
    public void execute(){
    	//log.info("[X]----------------------->>>验证任务时效...");
    	try{
    		List<MissionEntity> mlist =  missionDao.findActiveMission();
    		Date now = new Date();
    		if(mlist != null) {
    			for(MissionEntity mission : mlist){
    				if(now.after(mission.getEtime())) {
    					if(missionDao.stopmission(mission.getId(),"过时自动失效") == 0) {
    						log.error("[X]----------------------->>>更新任务状态失败...[" + mission.getLocksn() + ":" + mission.getCreatetime() + "]");
    					}
    					//更新任务锁具授权
    					if(lockDao.ResetLockUserInfoByMission(mission.getId()) == 0) {
    						log.error("[X]----------------------->>>任务终止,更新锁具授权状态失败...[" + mission.getLocksn() + ":" + mission.getCreatetime() + "]");
    					}
    					//删除柜员锁任务详情列表
    					if(missionDao.updateLockMissionDetail_reasonByid(mission.getId(),"过时自动时效") == -1) {
    						log.error("[X]----------------------->>>任务终止,更新任务详情列表失败...[" + mission.getCreatetime() + "]");
    					}
    				}
    			}
    		}
    	}catch(EmptyResultDataAccessException er){
    		log.error("[X]----------------------->>>验证任务时效异常...");
    	}

    }
    
    
    
    public static int differentDays(Date date1,Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);
        
        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //不同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年            
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }
            
            return timeDistance + (day2-day1) ;
        }
        else    //同一年
        {
            System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2-day1;
        }
    }


}